package com.sui.bigdata.rtcadmin.repository.mapper;

import com.sui.bigdata.rtcadmin.repository.model.JobConfig;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author YongChen
 * @date 2019/11/6 14:41
 * @description
 * @email yong_chen@sui.com
 */
@Mapper
@Component
public interface JobConfigMapper {

    @Select("select * from flink_sql_job_conf_info")
    @ResultType(JobConfig.class)
    List<JobConfig> queryAll();

    @Select("select * from flink_sql_job_conf_info where is_online = 1")
    @ResultType(JobConfig.class)
    List<JobConfig> queryOnlineJob();

    @Select("select * from flink_sql_job_conf_info where job_name = #{jobName}")
    @ResultType(JobConfig.class)
    JobConfig queryByJobName(@Param("jobName")String jobName);

    @Insert("insert into flink_sql_job_conf_info(job_name,developer,alarm_group_phone,exec_sql,parallelism,queue,job_manager_memory,task_manager_memory,slots,extend_param,description)\n" +
            "VALUES(#{jobName},#{developer},#{alarmGroupPhone},#{execSql},#{parallelism},#{queue},#{jobManagerMemory},#{taskManagerMemory},#{slots},#{extendParam},#{description})\n" +
            "ON DUPLICATE KEY UPDATE " +
            "job_name=#{jobName},developer=#{developer},alarm_group_phone=#{alarmGroupPhone},exec_sql=#{execSql},parallelism=#{parallelism},queue=#{queue}\n" +
            ",job_manager_memory=#{jobManagerMemory},task_manager_memory=#{taskManagerMemory},slots=#{slots},extend_param=#{extendParam},description=#{description}")
    void saveAll(JobConfig jobConfig);

    @Update("UPDATE flink_sql_job_conf_info SET `developer`=#{developer}, `alarm_group_phone`=#{alarmGroupPhone}, `description`=#{description}  WHERE job_name = #{jobName}")
    void updateAlarmByJobName(@Param("jobName") String jobName,@Param("developer") String developer, @Param("alarmGroupPhone") String alarmGroupPhone, @Param("description") String description);

    @Update("UPDATE flink_sql_job_conf_info SET `status`=#{status},`is_online`=#{isOnline} WHERE job_name = #{jobName}")
    void updateStatusByJobName(@Param("jobName") String jobName, @Param("status") String status, @Param("isOnline") int isOnline);

    @Select("select status from flink_sql_job_conf_info where job_name = #{jobName}")
    String queryStatusByJobName(@Param("jobName")String jobName);


    @Update("UPDATE flink_sql_job_conf_info SET `app_id`=#{appId}," +
            "`job_id`=#{jobId},`web_url`=#{webUrl},`status`=#{status},`is_online`=#{isOnline}  WHERE job_name = #{jobName}")
    void updateSubmitedByJobName(@Param("jobName") String jobName,
                                 @Param("appId") String appId,
                                 @Param("jobId") String jobId,
                                 @Param("webUrl") String webUrl,
                                 @Param("status") String status,
                                 @Param("isOnline") int isOnline);

    @Select("select job_id,app_id from flink_sql_job_conf_info where job_name = #{jobName}")
    @ResultType(JobConfig.class)
    JobConfig queryJobIdByJobName(@Param("jobName")String jobName);


    @Select("select job_name from flink_sql_job_conf_info where is_online = 1 and create_time >= #{startTime} and create_time <= #{endTime} ")
    @ResultType(JobConfig.class)
    List<JobConfig> queryJobByTime(@Param("startTime")String startTime ,@Param("endTime")String endTime);

    @Delete("delete from flink_sql_job_conf_info where is_online=0 and job_name = #{jobName}")
    void deleteByJobName(@Param("jobName")String jobName);
}
